package stack_queue;

import java.util.Stack;

/**
 * 232. 用栈实现队列
 *
 * @author Api
 * @date 2023/10/15 16:00
 */
public class Code232_UsingStackToRealizeQueue {
    // 两个栈实现一个队列
    static class MyQueue {
        Stack<Integer> stackIn;
        Stack<Integer> stackOut;
        public MyQueue() {
            stackIn = new Stack<>(); // 负责出栈
            stackOut = new Stack<>(); // 负责入栈
        }

        public void push(int x) {
            stackIn.push(x);
        }

        public int pop() {
            dumpStackIn();
            return stackOut.pop();
        }

        public int peek() {
            dumpStackIn();
            return stackOut.peek();
        }

        public boolean empty() {
            return stackIn.isEmpty() && stackOut.isEmpty();
        }
        // 如果stackOut为空，那么将stackIn中的元素全部放到stackOut中
        private void dumpStackIn(){
            if (!stackOut.isEmpty()){
                return;
            }
            while(!stackIn.isEmpty()){
                stackOut.push(stackIn.pop());
            }
        }
    }
}
